<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\CaseXqService;
use Illuminate\Http\Request;

class CaseXqController extends BaseController
{
    //原创案例-导览

    public function __construct(CaseXqService $caseService)
    {
        $this->db = $caseService;
    }

    //列表
    public function getIndex(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $cateid = $request->post('cate_id', 0);
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = $this->db->getIndex($cateid, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function getOne(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //修改
    public function modify(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $id = $request->post('id', 0);
        $pas['title'] = $request->post('title', '');
        $pas['cate_id'] = $request->post('cateid', 0);
        $cname = $request->post('cname', '');
        $pas['yaoqiu'] = $request->post('yaoqiu', '');
        $pas['price'] = $request->post('price', 0);
        $pas['start_day'] = $request->post('start_day', '');
        $pas['end_day'] = $request->post('end_day', '');
        $pas['price'] = $pas['price'] ? $pas['price'] : 0;
        $pas['item_detail'] = $request->post('cnt', '');
        if (!$pas['title']) {
            return $this->toJson(-2, '项目名称必须的');
        }
        if (!$pas['cate_id']) {
            return $this->toJson(-2, '项目分类必须的');
        }
        $pas['cid'] = $this->db->cidByCname($cname);
        if (!$pas['cid']) {
            return $this->toJson(-2, '公司不存在');
        }
        $pas['price'] = $pas['price'] * 100;
        if ($id) {
            $res = $this->db->modify($id, $pas);
        } else {
            $res = $this->db->add($pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        //header("Access-Control-Allow-Origin: *");
        $id = $request->post('id', 0);
        $res = $this->db->del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}